<?php
namespace Admin\Controller;
use Admin\Model\UserLogModel;

class CodeController extends MainController{

    /**
     * 发送短信验证码
     * @queyuan
     * 2016/05/31
     * @param $tel,$flag:0-注册，1-找回密码，2-验证码登录,$ip
     */
    public function sendCode() {
        $tel = I("param.tel");
        $flag = I("param.flag");
        $s = 0;
        $ip = I("param.ip");
        if(!$ip){
            $results = array(
                'status'=>1,
                'msg'=>'无法获取IP',
            );
        }else{
            $time_begin = strtotime(date("Y-m-d 00:00:00",time()));
            $time_end = strtotime(date("Y-m-d 23:59:59",time()));
            $count_ip = M("user_code")->where("ip = '".$ip."' and add_time > ".$time_begin." and add_time < ".$time_end)->count();
            $count_tel = M("user_code")->where("phone = '".$tel."' and add_time > ".$time_begin." and add_time < ".$time_end)->count();
            if($count_ip >= 3){
                $results = array(
                    'status'=>1,
                    'msg'=>'已达上限',
                );
            }else{
                if($count_tel >= 3){
                    $results = array(
                        'status'=>1,
                        'msg'=>'已达上限',
                    );
                }else{
                    if(!$s){
                        $time = M('user_code')->where("phone = '".$tel."'' and flag = ".$flag)->getField('add_time');
                        if($time){
                            $m = (time()-$time)/60;
                            if($m <= 1){
                                $s = 1;
                                $results = array(
                                    'status' => 1,
                                    'msg' => '请一分钟后再试',
                                );
                            }else{
                                $s = 0;
                            }
                        }
                        if(!$s){
                            $code = rand('100000', '999999');
                            $sms = send_checkcode($tel,$code);
                            $count = M('user_code')->where("phone = '".$tel."'")->count();
                            $data = array(
                                'phone' => $tel,
                                'code' => $code,
                                'flag' => $flag,
                                'add_time' => time(),
                                'ip'=>$ip
                            );
                            M("user_code")->add($data);
                            if($sms == '00'){
                                $results = array(
                                    'status' => 0,
                                    'msg' => '发送成功'
                                );
                            }else{
                                $results = array(
                                    'status' => 1,
                                    'msg' => '发送失败'
                                );
                            }
                        }
                    }
                }
            }
        }
        echo json_encode($results);
        exit;
    }

    /**
     * 检查验证码
     * @queyuan
     * 2016/05/31
     * @param $phone,$flag,$code
     */
    public function checkCode(){
        $phone = I("param.phone");
        $flag = I("param.flag");
        $code = I("param.code");
        $result = array();
        if(!$phone || !$code){
            $result['status'] = 1;
            $result['msg'] = "参数错误";
        }else{
            $count = M("user_code")->where("phone = '".$phone."' and flag = ".$flag." and code = '".$code."'")->count();
            if($count){
                $result['status'] = 0;
                $result['msg'] = "验证成功";
            }else{
                $result['status'] = 1;
                $result['msg'] = "验证码错误";
            }
        }
        echo json_encode($result);
        exit;
    }
}
?>